package com.crazydream.wechat.system.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.crazydream.wechat.common.model.TableEntity;

@Entity
@NamedQueries( { @NamedQuery(name = Role.QUERY_ROLE_BY_RESOURCE, query = "select t from Role t join t.resourceList r where r.id =:id") })
@Table(name = "t_role")
public class Role extends TableEntity{

	private static final long serialVersionUID = 6824643230577126498L;
	
	/**
	 * 根据资源ID查询有这个资源的角色
	 */
	public static final String QUERY_ROLE_BY_RESOURCE = "Role.queryRoleByResource";

	private String name;

	private String descr;

	private Integer enabled;

	@Temporal(TemporalType.TIMESTAMP)
	private Date createdate;
	
	@ManyToMany(targetEntity=Resource.class,fetch=FetchType.EAGER)
	@JoinTable(name = "t_role_resource",
			   joinColumns = {@JoinColumn(name = "roleid")} ,
			   inverseJoinColumns = {@JoinColumn(name = "resourceid")})
	private Set<Resource> resourceList;
	
	
	public Set<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(Set<Resource> resourceList) {
		this.resourceList = resourceList;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	public Integer getEnabled() {
		return enabled;
	}

	public void setEnabled(Integer enabled) {
		this.enabled = enabled;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}
}
